const axios = require('axios')
const puppeteer = require('puppeteer')
const cheerio = require('cheerio')
const { Mobile, initDataBase } = require('./database')
const baseUrl = 'http://www.aihuishou.com/userinquiry/create.html'
const baseHost = 'https://www.aihuishou.com'

function getPriceUnits(props) {
  let PriceUnits = ''
  props.forEach(item => {
    PriceUnits += item.value + ';'
  })
  PriceUnits = PriceUnits.slice(0, PriceUnits.length - 1)
  return PriceUnits
}
function genUrl(uAsec, AuctionProductId, PriceUnits) {
  return `${baseUrl}?u_asec=${uAsec}&u_atype=2&AuctionProductId=${AuctionProductId}&PriceUnits=${PriceUnits}`
}

async function run(params) {
  await initDataBase()
  const mobiles = await Mobile.limit(100)
    // .skip((pageIndex - 1) * 100)
    .find({ price: null })
  const browser = await puppeteer.launch({ headless: false })
  // const datastr = fs.readFileSync('./data.json')
  // const models = JSON.parse(datastr)

  const page = await browser.newPage()

  for (let i = 0; i < 1; i++) {
    const mobile = mobiles[i]

    let productId = mobile.get('productId')
    let props = mobile.get('props').map(item => item.value)

    const PriceUnits = props.join(';')
    let initUrl = `https://www.aihuishou.com/product/${productId}.html?priceValues=${props.join(',')}`
    console.log('initUrl', initUrl)
    await page.goto(initUrl)

    // const cookies = await page.cookies(baseHost)
    // let uAsec = cookies.find(item => item.name === 'u_asec').value
    // console.log(' uAsec', uAsec)
    let logval = await page.evaluate(() => {
      // UA_Opt.Token = 'AAAA'
      // UA_Opt.reload()
      let ua = encodeURIComponent(getUA())
      console.log(ua)
      return ua
    })
    // uAsec = encodeURIComponent(uAsec)
    // console.log('logval', logval)
    logval =
      '099%23KAFE2EEpEqQEhGTLEEEEEpEQz0yFD6tHZXn5S6g1DXR7W6PTSr%2FEZ6zJDc7TEE7EYRpCBYFETEEEbOKhE7Eht3alluZdAYFEqEEkbOpdCwUQrjDt9235rAXGPqG3%2FoQSPsUB%2BUZVNLYVPaI3%2Be138Kb2haQt%2Fq%2BZ6NGTEELStETtNMjzSuQTEEyZtY7IZQlbE7EUt3ilBcZdtqRStT1gsyaDf%2FiSFJsP%2F3fat377UXZdtVOStT1gsyan8rYTEHIEcWqEjrV%2BMJK41ESAzZjDfJcy1hWcqHGt060qm%2FN4kL0oiSYsuBqt6w16w%2BXLy8gNIEac2RjC01EDLzArUBjayCT0DLcI3kj2qNoRyUQCBwD4lYFETKxqAjJ5E7EFt37EFE%3D%3D'
    let url = genUrl(logval, productId, PriceUnits)
    console.log(url)
    const { data: priceUrl } = await axios({
      url: url,
      method: 'post',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent':
          'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36'
      }
    })
    console.log(priceUrl)
    if (priceUrl.data.redirectUrl) {
      const fullUrl = `${baseHost}${priceUrl.data.redirectUrl}`
      console.log(fullUrl)
      const { data: priceHtml } = await axios({ url: fullUrl })
      const $ = cheerio.load(priceHtml)
      const price = $('#quoter-section').data('price')
      console.log(price)
    } else {
      console.log(priceUrl)
    }
  }
}

run()
